// pages/register/register.js
var app=getApp();
Page({
    data:{
        url:'',
        p1:true,
        p2:true,
        register:{
            username:'',
            password:''
        },
        password:'',
        but: true,
        check: false,
    },

    onLoad(options) {
        // 获取全局的url赋值给当前页面的URL变量
        this.setData({
            url : app.globalData.URL
         })
    },

    changeCheck(){
        this.data.check=!this.data.check;
        //console.log(this.data.check);
        if(this.data.register.password==this.data.password&&this.data.check){
            this.setData({
                'but' : false 
            })
        }else{
            this.setData({
                'but' : true 
            })
        }
    },

    username: function (e) {
        this.setData({
            'register.username' : e.detail.value
          });
        if(this.data.register.username!=''&&this.data.register.username!=null){
            this.setData({
                'p1' : false
              });
        }
        if(this.data.register.username==''||this.data.register.username==null){
            this.setData({
                'p1' : true
              });
            this.setData({
                'register.password':null
              });
        }  
    },


    pv1: function (e) {
        this.setData({
          'register.password' : e.detail.value
        });
        if(this.data.register.password!=''&&this.data.register.password!=null){
            this.setData({
                'p2' : false
              });
              this.setData({
                  'but': true
              })
        }
        if(this.data.register.password==''||this.data.register.password==null){
            this.setData({
                'p2' : true
              });
            this.setData({
                'password':null
              });
        }  
      },
    pv2: function (e) {
        this.setData({
            'password' : e.detail.value
        });
        if(this.data.register.password==this.data.password&&this.data.check){
            this.setData({
                'but' : false 
            })
        }else{
            this.setData({
                'but' : true
            })
        }
      }, 

    registerUser(){
        let that = this

        that.setData({
            'but' : true 
        })

        if(that.data.register.username==''||that.data.register.username==null){
            that.setData({
                'loginInf.username' : ' '
            })
        }
        if(that.data.register.password==''||that.data.register.password==null){
            that.data.register.password.length
            //console.log(that.data.register.password.length);
            that.setData({
                'loginInf.password' : null
            })
        }

        if(that.data.register.username.length<4){
            wx.showModal({
                title: '用户名格式错误',
                content: '用户名长度太短，请重试！',
                success: function (res) {
                    that.setData({
                        'but' : false 
                    })
                    if (!res.confirm) {
                        that.setData({
                            'register.username' : '',
                            'register.password' : ''
                        });
                    } 
                }
            });
            return;
        }

        if(that.data.register.password.length<5){
            wx.showModal({
                title: '密码格式错误',
                content: '密码长度太短，请重试！',
                success: function (res) {
                    that.setData({
                        'but' : false 
                    })
                    if (!res.confirm) {
                        that.setData({
                            'register.username' : '',
                            'register.password' : ''
                        });
                    } 
                }
            });
            return;
        }
        
        //console.log("111",that.data.register.username);
        wx.request({
          url: that.data.url+'user/findUserByUsername/?username='+that.data.register.username,
          method:'GET',
          success:function (res) {
              console.log(res.data.code);
            if(res.data.code==1){
                wx.showModal({
                    title: '用户名错误',
                    content: '用户名已存在，请重试！',
                    success: function (res) {
                        that.setData({
                            'but' : false 
                        })
                        if (!res.confirm) {
                            that.setData({
                                'register.username' : '',
                                'register.password' : '',
                                'password' : ''
                            });
                        }
                        return; 
                    }
                });
            }else{
                wx.request({
                    url: that.data.url + 'user/addUser', // 替换为实际的请求地址
                    method: 'POST',
                    data: that.data.register,
                    header: {
                        'content-type': 'application/json' // 设置请求头，指定数据格式为 JSON
                    },
                    success: function (res) {
                        if(res.data.code==1){
                            //console.log(111);
                            console.log(res.data.data);
                            wx.redirectTo({
                              url: '/pages/mine/login/login',
                            })
                        }else{
                            that.setData({
                                'but' : false 
                            })
                            console.log("注册失败");
                        }
                    },
                    fail: function (err) {
                        that.setData({
                            'but' : false 
                        })
                        console.error('请求失败', err);
                    }
                });
            }
          }
        })
    }
})